package com.software.room;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.software.room.dao.UserInfoDao;
import com.software.room.database.UserInfoDatabase;
import com.software.room.entity.UserInfo;

public class MainActivity extends AppCompatActivity {

    private EditText edt_weight;
    private EditText edt_height;
    private EditText edt_age;
    private EditText edt_name;
    private Button btn_insert;;
    private Button btn_find_id;
    private EditText edt_find_id;
    private Button btn_query;
    private EditText edt_del_id;
    private Button btn_delete;
    private Button btn_update;
    private UserInfoDao userInfoDao;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initEvents();
    }

    private void initEvents() {
        btn_insert.setOnClickListener(v -> {
            UserInfo userInfo = new UserInfo();
            userInfo.setName(edt_name.getText().toString());
            userInfo.setAge(Integer.valueOf(edt_age.getText().toString()));
            userInfo.setHeight(Integer.valueOf(edt_height.getText().toString()));
            userInfo.setWeight(Float.valueOf(edt_weight.getText().toString()));
            userInfoDao.insert(userInfo);
            Toast.makeText(this, "新增成功", Toast.LENGTH_SHORT).show();
        });


    }

    @Override
    protected void onStart() {
        super.onStart();
        userInfoDao = UserInfoDatabase.getInstance(this).getUserInfoDao();

    }

    private void initViews() {
        edt_name = findViewById(R.id.edt_name);
        edt_age = findViewById(R.id.edt_age);
        edt_height = findViewById(R.id.edt_height);
        edt_weight = findViewById(R.id.edt_weight);

        btn_insert = findViewById(R.id.btn_insert);

        edt_find_id = findViewById(R.id.edt_find_id);
        btn_find_id = findViewById(R.id.btn_find_id);

        btn_query = findViewById(R.id.btn_query);

        edt_del_id = findViewById(R.id.edt_del_id);
        btn_delete = findViewById(R.id.btn_delete);

        btn_update = findViewById(R.id.btn_update);
    }
}